package com.dz.Incubator.controller;

import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.dto.ServiceInfoDto;
import com.dz.Incubator.entity.InvokeResult;
import com.dz.Incubator.entity.ServiceInfoEntity;
import com.dz.Incubator.service.ServiceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/api/a/zx/serviceInfo")
@Slf4j
public class ServiceInfoController {

    @Autowired
    private ServiceInfoService serviceInfoService;

    /**
     * 分页查询 serviceInfo 对象
     *
     */
    @RequestMapping(value = "/getPageList", method = RequestMethod.POST)
    @ResponseBody
    public InvokeResult<Pagination> getPageList(@RequestBody Pagination pagination) {
        InvokeResult invokeResult = null;
        try {
            if (null == pagination) {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
            Pagination pageInfo = serviceInfoService.findByPagination(pagination);
            if (null != pageInfo && null != pageInfo.getList() && pageInfo.getList().size() > 0) {
                invokeResult = InvokeResult.success(pageInfo);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 获取单个ServiceInfoEntity 对象
     *
     * @param id Id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<ServiceInfoEntity> get(@PathVariable Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null != id) {
                ServiceInfoEntity serviceInfoEntity = serviceInfoService.get(id);
                if (null == serviceInfoEntity) {
                    invokeResult = InvokeResult.pass_no_data();
                }
                invokeResult = InvokeResult.success(serviceInfoEntity);
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 删除单个serviceInfoEntity 对象
     *
     * @param id 删除数据 ID
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult delete(@PathVariable Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null != id) {
                serviceInfoService.delete(id);
                invokeResult = InvokeResult.success();
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 集合查找 serviceInfoEntity 对象
     *
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<List<ServiceInfoEntity>> find(@RequestParam("ids") List<Long> ids) {
        InvokeResult invokeResult = null;
        try {
            if (null != ids && ids.size() > 0) {
                Long[] skuIds = ids.toArray(new Long[ids.size()]);
                List<ServiceInfoEntity> serviceInfoEntitys = serviceInfoService.find(skuIds);
                if (null != serviceInfoEntitys && serviceInfoEntitys.size() > 0) {
                    invokeResult = InvokeResult.success(serviceInfoEntitys);
                } else {
                    invokeResult = InvokeResult.pass_no_data();
                }
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 查找所有 serviceInfoEntity 对象
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ServiceInfoEntity>> findAll() {
        InvokeResult invokeResult = null;
        try {
            List<ServiceInfoEntity> serviceInfoEntitys = serviceInfoService.findAll();
            if (null != serviceInfoEntitys && serviceInfoEntitys.size() > 0) {
                invokeResult = InvokeResult.success(serviceInfoEntitys);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 新增数据
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<ServiceInfoEntity> add(@RequestBody ServiceInfoEntity serviceInfoEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != serviceInfoEntity) {
                invokeResult = InvokeResult.success(serviceInfoService.insert(serviceInfoEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 修改数据
     *
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<ServiceInfoEntity> update(@RequestBody ServiceInfoEntity serviceInfoEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != serviceInfoEntity && null != serviceInfoEntity.getId()) {
                invokeResult = InvokeResult.success(serviceInfoService.update(serviceInfoEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }


    /**
     * 查询服务项目信息（多级菜单）
     */
    @RequestMapping(value = "/menu", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<ServiceInfoDto> getMenu() {
        InvokeResult invokeResult;
        try {
            List<ServiceInfoDto> serviceInfoEntitys = serviceInfoService.getMenu();
            if (null != serviceInfoEntitys && serviceInfoEntitys.size() > 0) {
                invokeResult = InvokeResult.success(serviceInfoEntitys);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    @RequestMapping(value = "findRecomend", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ServiceInfoEntity>> findRecomend() {
        return InvokeResult.success(serviceInfoService.findRecomend());
    }

    @RequestMapping(value = "searchService", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ServiceInfoEntity>> searchService(String key, int type) {
        return InvokeResult.success(serviceInfoService.searchService(key, type));
    }

}